package com.web.portal.controller;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.web.portal.entity.Admin;
import com.web.portal.service.AdminService;
import com.web.portal.validator.LoginValidator;

@Controller
public class LoginController {
	private static final Logger LOG = Logger.getLogger("LoginController");
	@Autowired
	AdminService adminService;
	@Autowired
	private LoginValidator loginValidator;

	public AdminService getAdminService() {
		return adminService;
	}

	@InitBinder
	protected void initBinder(WebDataBinder binder) {

		binder.setValidator(loginValidator);
	}

	public void setAdminService(AdminService adminService) {
		this.adminService = adminService;
	}

	@RequestMapping(value = "/login.do", method = RequestMethod.GET)
	public String admin(Map model, @RequestParam(value = "error", required = false) String error) {
		/*Admin admin = new Admin();
		model.put("admin", admin);*/
		if (error != null) {
			model.put("error", "Invalid username and password!");
		}
		
		LOG.debug("Calling admin_login page");
		return "admin_login";
	}
	 @RequestMapping(value = "/accessdenied", method = RequestMethod.GET)
	    public String loginerror(ModelMap model) {
	        model.addAttribute("error", "true");
	        return "denied";
	    }

	@RequestMapping(value = "/admin/success_login.do", method = RequestMethod.GET)
	public String processLoginCredentials(@Valid Admin admin,
			BindingResult result, Map model, HttpServletRequest request) {

		/*if (result.hasErrors()) {
			admin.setUserName("");
			admin.setPassword("");
			model.put("admin", admin);
			return "admin_login";
		}
		
		 * admin = (Admin) model.get("admin"); if
		 * (!adminService.isUserNamePasswordValid(admin.getUserName(),
		 * admin.getPassword())) { admin.setUserName(""); admin.setPassword("");
		 * model.put("admin", admin); return "admin_login"; }
		 */
/*
 * set session for admin
 */
		/*boolean isValid = true;
		request.getSession().setAttribute("isValid", isValid);*/
		LOG.debug("Calling faculty_and staff page........");
		return "admin_manager";
	}

}
